package common

import (
	"fmt"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"

	"gitee.com/zoneaurora/gin_example/model"
)

var db *gorm.DB

func InitDB() {
	// driverName := "mysql"
	host := "172.17.0.2"
	port := "3306"
	datebase := "gin_example"
	username := "root"
	password := "Config123"
	charset := "utf8"
	args := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local",
		username,
		password,
		host,
		port,
		datebase,
		charset)
	d, err := gorm.Open(mysql.Open(args), &gorm.Config{})
	if err != nil {
		panic("failed to connect database, err: " + err.Error())
	}
	db = d
	db.AutoMigrate(&model.User{})
}

func DB() *gorm.DB {
	return db
}
